﻿/* 
 * 
 * Copyright 2013 Bendsoft. All rights reserved.
 * http://www.bendsoft.com
 * 
 * This code is distributed under the New BSD License (BSD).
 * 
 * @package     Camelot SharePoint Integration Library
 * @codeplex    http://camelottoolkit.codeplex.com
 * @license     New BSD License (BSD)
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Camelot.SharePointIntegration.Client
{
    public class DocumentLibraryNode : IHierarchyData
    {
        internal DocumentLibraryNodeCollection Source { get; set; }

        public int Id { get; set; }
        public Guid Guid { get; set; }
        public string Name { get; set; }
        public string Url { get; set; }
        public string ParentUrl { get; set; }
        public string ContentType { get; set; }
        public string FileType { get; set; }
        public int FileSize { get; set; }
        public DateTime Created { get; set; }
        public string CreatedBy { get; set; }
        public DateTime Modified { get; set; }
        public string ModifiedBy { get; set; }
        public string CheckedOutBy { get; set; }
        public string ProcedureUrl { get; set; }
        public int Level { get; set; }

        public DocumentLibraryNode() { }

        public IHierarchicalEnumerable GetChildren()
        {
            var children = new DocumentLibraryNodeCollection();

            foreach (var node in this.Source)
            {
                if (node.ParentUrl == this.Url)
                {
                    children.Add(node);
                }
            }
            return children;
        }

        public IHierarchyData GetParent()
        {
            foreach (var node in this.Source)
            {
                if (node.Url == this.ParentUrl)
                    return node;
            }
            return null;
        }

        public bool HasChildren
        {
            get
            {
                foreach (var node in this.Source)
                {
                    if (node.ParentUrl == this.Url)
                    {
                        return true;
                    }
                }
                return false;
            }
        }

        public object Item
        {
            get { return this; }
        }

        public string Path
        {
            get { return this.Url; }
        }

        public string Type
        {
            get { return this.ContentType; }
        }

        public override string ToString()
        {
            return this.Name;
        }
    }

    public class DocumentLibraryNodeCollection : List<DocumentLibraryNode>, IHierarchicalEnumerable
    {
        public DocumentLibraryNodeCollection() : base() { }

        public IHierarchyData GetHierarchyData(object enumeratedItem)
        {
            return enumeratedItem as IHierarchyData;
        }

        public new void Add(DocumentLibraryNode item)
        {
            if (item.Source == null)
                item.Source = this;
            base.Add(item);
        }

        public DocumentLibraryNodeCollection GetRootDocuments()
        {
            var root = new DocumentLibraryNodeCollection();
            root.AddRange(this.Where(doc => doc.Level == 0));
            return root;
        }
    }
}
